<?php
//PingBuWang.Com [Don't forpost the beginner's mind]
//Copyright (c) 2017~2099 http://www.pingbuwang.com All rights reserved.
namespace app\user\controller;
use mip\Mip;
use Think\Controller;
use app\user\model\Users;
use app\common\controller\Common;
use think\Request;
use think\Session;
use com\SmsUtils;

class User extends Common
{

    /**
     * 短信验证
     */
    public function message_post(){
        $mobile=input('post.phone');
        $user=session('user');
        if(empty($user)){
            return array('code'=>-1,'msg'=>'请登录账号');
        }
        if(preg_match("/^1[34578]\d{9}$/", $mobile)){
            $timestamp=session('timestamp');
            $timestamp1=time();
            $timestamp2=$timestamp1-$timestamp;
            if(!empty($timestamp)&&$timestamp2<60){
                return array('code'=>-1,'msg'=>'发送短信间隔60s');
            }else{
                $num=rand(100000,999999);
                $content='验证码：'.$num.'，如非本人操作，请忽略本短信。';
                $controller=$this;
                $send=\com\SmsUtils::send($mobile,$content,$controller);
                if($send){
                    session('mobile_message',$num);
                    session('timestamp',time());
//                    10分钟内可以发5次,超过5次等待30分钟才能发送
                }
            }

        }else{
            return jsonError('请填写正确的手机号');
        }
    }
    /**
     * 登陆
     */
    public function login(){
        return $this->mipView('user/login');
    }
    public function logindo() {
        if (!IS_POST) {
            halt("页面不存在");
        }
        $users = db("users");
        $username = input("post.username");
        $password = input("post.password");
        $code = input("post.code");

        if (md5($code) != session("verify")) {
            $this->error("验证码输入错误");
        }

        $rs = $users->where("username" ,$username)->find();

        if (!empty($rs)) {
            if (create_md5($password,$rs['salt'])!= $rs['password']) {
                return jsonError('用户名或者密码错误');
            } else {
                session("user_uid", $rs["uid"]);
                $this->success("登陆成功", url("user/user/gerenzhongxin"));
            }
        } else {
            return jsonError('当前用户不存在');
        }
    }
    /**
     * 注册
     */
    public function reg(){
        return $this->mipView('user/register');
    }
    public function regdo_ajax(){

        $password = input('post.password');
        $username=input('post.username');

        if($username==''){
            return jsonError('用户名为空');
        }
        $user_count=db('users')->where('username',$username)->count();
        if($user_count>0){
            return jsonError('当前用户已被注册');
        }
        if($password==null||$password==''){
            return jsonError('新增用户的时候，必须填写密码');
        }
        if($password!=null&& $password!=''){
            $data['salt'] = create_salt(8);
            $npassword1 = create_md5($password,$data['salt']);
            $data['password'] = $npassword1;
        }
        $data['username'] = $username;
        $data['nickname'] =  input('post.nickname');
//        $data['head_pic'] = input('post.head_pic');
        $data['uid'] = uuid();
        $data['group_code'] = 100;
        $data['group_id'] = 1;
        $data['login_num'] = 1;
        $data['reg_ip'] = request()->ip();
        $data['reg_time'] = time();
        $data['last_login_ip'] = request()->ip();
        $data['last_login_time'] = time();
        $data['terminal'] = 'pc';
        if ($data) {
            $usersInfo = db('users')->insert($data);
            if($usersInfo){
                session("user_uid", $data['uid']);
                $this->success("登陆成功", url("user/user/gerenzhongxin"));
            }
//            return jsonSuccess('用户保存成功');
        }
    }


}